//
//  AttentionViewController.m
//  FKGW
//
//  Created by song ce on 2017/2/20.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "AttentionViewController.h"
#import "AttentionCell.h"
#import "StoreViewController.h"
@interface AttentionViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableV;
@property(nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation AttentionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if(!ISNOTNEEDMESSAGEBTN){
        self.isNeedMessageItem = YES;
    }
    self.dataSource = [NSMutableArray array];
    [self tableViewAndHeaderView];
    [self loadData];

}

-(void)loadData{
    //2.57 我的关注接口
    NSDictionary *paraDic = @{
                              @"userId":USERID,
                              };
    __weak typeof(self)  weakSelf = self;
    [MBProgressHUD showLoadingGif];
    [Tools AFNetworkingRequestTypePostWithUrl:MYATTENTION paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@" 我的关注接口---%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSArray *data = resultDic[@"data"];
            if([data isKindOfClass:[NSNull class]]){
                [MBProgressHUD dismissHUD];
                return ;
            }
            for(NSDictionary *dic  in data){
                MyAttentionModel *model = [MyAttentionModel modelWithDic:dic];
                [weakSelf.dataSource addObject:model];
            }
            if(!weakSelf.dataSource.count){
                [MBProgressHUD dismissHUD];
                [weakSelf.view addSubview:weakSelf.noDataIUmageV];
                return;
            }

        //刷新页面
         [weakSelf.tableV reloadData];
         weakSelf.tableV.hidden = NO;
         [MBProgressHUD dismissHUD];
         
        }else{
            [MBProgressHUD dismissHUD];
            if(resultDic[@"errorString"]){
                [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
            }
        }
    }];
}

- (void)tableViewAndHeaderView{
    //表视图
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviBarHeight, Main_Screen_Width, Main_Screen_Height-kNaviBarHeight) style:UITableViewStylePlain];
    _tableV.showsVerticalScrollIndicator = NO;
    _tableV.backgroundColor = DEF_COLOR242;
    _tableV.delegate = self;
    _tableV.dataSource =self;
    _tableV.separatorStyle =UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableV];
    _tableV.tableFooterView = [[UIView alloc]init];
    _tableV.hidden = YES;
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ident=@"actell";
    AttentionCell *cell=[tableView dequeueReusableCellWithIdentifier:ident];
    if(cell==nil){
        cell=[[[NSBundle mainBundle]loadNibNamed:@"AttentionCell" owner:self options:nil]lastObject];
    }
    MyAttentionModel *model = self.dataSource[indexPath.row];
    
//    __weak typeof (self) weakSelf = self;
    __weak typeof (cell) weakCell = cell;
    cell.mySwipeBlock = ^{
        for(AttentionCell *tmpCell in tableView.visibleCells){
            // 将其他正在打开的cell关闭
            if (![weakCell isEqual:tmpCell]) {
                [tmpCell closeMenu];
            }
            
            // 所有cell取消选中状态
            tmpCell.selected = NO;
        }
    };
    
     //取消关注
    cell.myDeleteBlock =^(){
        DLog(@"shanchu");
       // 2.58 取消关注接口
        NSDictionary *paraDic = @{
                                  @"userId":USERID,
                                  @"storeId":model.storeId
                                  };
//            __weak typeof(self)  weakSelf = self;
        [Tools AFNetworkingRequestTypePostWithUrl:DELMYATTENTION paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
            if([resultDic[@"flag"] isEqualToString:@"true"]){
                [MBProgressHUD showTipMessageInView:@"取消成功"];
                //回到主线程刷新UI
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (self.dataSource.count) {
                        [self.dataSource removeObjectAtIndex:indexPath.row];
                        [self.tableV reloadData];
                        if(!self.dataSource.count){
                            self.tableV.hidden = YES;
                            return ;
                        }
//                        if (indexPath.row == self.dataSource.count) {
//                            // 删除的是最后一个直接刷新
//                            [self.tableV reloadData];
//                        }else{
//                            // 删除其他的cell，带动画
//                            [self.tableV deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
//                            [self.tableV reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
//                        }
                    }
                });
            }else{
                if(resultDic[@"errorString"]){
                    [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
                }
            }
        }];
    };
    cell.model = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyAttentionModel *model = self.dataSource[indexPath.row];
    StoreViewController *goodsDetail  = [[StoreViewController alloc]init];
    goodsDetail.storeId =model.storeId ;
    goodsDetail.vcName = model.storeName;
    [self.navigationController pushViewController:goodsDetail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
